import time

from selenium.webdriver.common.by import By

from common.base import Base
from common.functions import open_browser
from page.login_page import LoginPage


class OrderManagePage(Base):
    back_login_url = 'http://139.129.26.163/admin/'

    # 切换iframe方法
    def switch_to_frame1(self):
        self.base_switch_iframe((By.ID, 'menu-frame'))

    # 点击订单管理按钮
    def click_manage(self):
        self.base_click((By.XPATH, '//*[@id="menu-ul"]/li[4]'))

    # 回到主页ifrme
    def switch_to_default_frame(self):
        self.base_switch_default_iframe()

    # 切换iframe
    def switch_to_frame2(self):
        self.base_switch_iframe((By.ID, 'main-frame'))

    # 定位订单号input
    def input_ordernum(self, number):
        self.base_send_keys((By.XPATH, '//*[@id="order_sn"]'), number)

    # 定位收货人input
    def input_consignee(self, name):
        self.base_send_keys((By.ID, 'consignee'), name)

    # 定位订单状态下拉框
    def select_order_status(self, text):
        self.base_select_text((By.ID, 'status'), text)

    # 定位搜索按钮并点击
    def but_search(self):
        self.base_click((By.XPATH, '/html/body/div[1]/form/input[3]'))

    # 定位高级搜索并点击
    def but_adv_search(self):
        self.base_click((By.XPATH, '/html/body/div[1]/form/span'))

    # 定位订单号
    def input_order_num(self, num):
        self.base_send_keys((By.XPATH, '/html/body/div[2]/form/div/div[2]/table/tbody/tr[1]/td[2]/input'), num)

    # 高级搜索收货人元素定位
    def input_order_consignee(self, name):
        self.base_send_keys((By.XPATH, '/html/body/div[2]/form/div/div[2]/table/tbody/tr[3]/td[4]/input'), name)

    # 电子邮箱定位
    def email(self, email):
        self.base_send_keys((By.ID, 'email'), email)

    # 购货人定位
    def buyer(self, name):
        self.base_send_keys((By.ID, 'user_name'), name)

    # 收货人定位
    def consignee(self, name):
        self.base_send_keys((By.ID, 'consignee'), name)

    # 地址定位
    def addr(self, adder):
        self.base_send_keys((By.ID, 'address'), adder)

    # 邮编定位
    def zipcode(self, zipcode):
        self.base_send_keys((By.ID, 'zipcode'), zipcode)

    # 电话定位哦
    def tel(self, tel):
        self.base_send_keys((By.ID, 'tel'), tel)

    # 手机定位
    def mobile(self, mobile):
        self.base_send_keys((By.ID, 'mobile'), mobile)

    # 下拉框所在地区
    # 国家
    def country(self, text):
        self.base_select_text((By.ID, 'selCountries'), text)

    # 省份
    def province(self, text):
        self.base_select_text((By.ID, 'selProvinces'), text)

    # 城市
    def city(self, text):
        self.base_select_text((By.ID, 'selCities'), text)

    # 区
    def district(self, text):
        self.base_select_text((By.ID, 'selDistricts'), text)

    # 下拉框配送方式
    def mode_of_distribution(self, text):
        self.base_select_text((By.ID, 'select4'), text)

    # 支付方式下拉框
    def mode_of_payment(self, text):
        self.base_select_text((By.ID, 'select5'), text)

    # 订单状态下拉框
    def order_status(self, text):
        self.base_select_text((By.ID, 'select9'), text)

    # 付款状态下拉框
    def payment_status(self, text):
        self.base_select_text((By.ID, 'select11'), text)

    # 发货状态下拉框
    def shipping_status(self, text):
        self.base_select_text((By.ID, 'select10'), text)

    # 下单时间下拉框
    # 使用JS remove readonly attribute，然后使用sendKeys 方法 ，输入正确格式的日期
    def promote_start_date(self, content):
        # 去除只读属性
        js_pha = "document.getElementById('start_time_id').removeAttribute('readonly', '')"
        self.base_execute_script(js_pha)
        # 清除原来的内容并写入日期
        # self.base_find_element((By.ID,"start_time_id")).clear()
        # self.base_find_element((By.ID,"start_time_id")).sendKeys(content)
        self.base_send_keys((By.ID, "start_time_id"), content)

    # 时间下拉框
    def promote_over_date(self, content):
        # 去除只读属性
        js_pha = "document.getElementById('end_time_id').removeAttribute('readonly', '')"
        self.base_execute_script(js_pha)
        # 清除原来的内容并写入日期
        # self.base_find_element((By.ID,"end_time_id")).clear()
        # self.base_find_element((By.ID,"end_time_id")).sendKeys(content)
        self.base_send_keys((By.ID, "end_time_id"), content)

    def font_text(self):
        return self.base_get_element_text((By.ID, 'order_0'))

    # 高级搜索点击搜索
    def click_advanced_search(self):
        self.base_click((By.ID, 'query'))


if __name__ == '__main__':
    driver = open_browser('chrome')
    order = OrderManagePage(driver)
    order.base_get('http://139.129.26.163/admin/')
    time.sleep(2)
    order.base_send_keys((By.NAME, 'username'), 'admin')
    order.base_send_keys((By.NAME, 'password'), 'admin123456')
    order.base_click((By.CLASS_NAME, 'btn-a'))
    time.sleep(2)
    order.base_switch_iframe((By.ID, 'menu-frame'))
    order.click_manage()
    order.base_switch_default_iframe()
    order.base_switch_iframe((By.ID, 'main-frame'))
    # order.input_ordernum(2023052101676)
    # order.input_consignee(123)
    # order.but_search()
    order.but_adv_search()
    time.sleep(2)
    # order.email('154@qq.com')
    order.country('中国')
    order.click_advanced_search()
    time.sleep(2)
